<?php
namespace app\admin\controller;

use app\admin\model\WelfareType as WelfareTypeModel;
use Request;

/**
 * 福利类型操作，主要金币元宝类操作
 * WelfareType/index 显示
 * WelfareType/add  添加
 * WelfareType/edit 修改
 * WelfareType/delete 删去
 * WelfareType/quickedit 快捷状态
 */
class WelfareType extends Base
{

    public $data;
    /**
     * 显示文章树形分类列表
     * @return array 文章分类二维数组
     */
    public function index()
    {

        $list = WelfareTypeModel::select();
        $this->assign('list', $list);
        return $this->fetch();
    }

    /**
     * 添加分类信息
     */
    public function add()
    {
        if ($this->request->isPost()) {

            $post = $this->request->post();

            $result = $this->validate($post, 'app\admin\validate\WelfareType');
            if (true !== $result) {
                return $this->error($result);
            }

            $data = WelfareTypeModel::create($post);
            if (!empty($data)) {
                return $this->success('添加成功', 'admin/WelfareType/index');
            }
        }
        return $this->fetch();
    }

    /**
     * 修改分类信息
     * @return [type] [description]
     */
    public function edit()
    {
        $id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
        if ($this->request->isPost()) {

            $post = $this->request->post();

            $result = $this->validate($post, 'app\admin\validate\WelfareType');
            if (true !== $result) {
                return $this->error($result);
            }

            $model = WelfareTypeModel::update($post);
            if ($model) {
                return $this->success('修改成功', 'admin/WelfareType/index');
            } else {
                return $this->error('修改失败');
            }
        }

        $info = WelfareTypeModel::get($id);
        if (empty($info)) {return $this->error('id参数错误');}
        $this->assign([
            'info'     => $info,
        ]);
        return $this->fetch();
    }

    /**
     * 删去分类信息
     * @param  string $value [description]
     * @return [type]        [description]
     */
    public function delete($value = '')
    {
        $id   = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
        $data = WelfareTypeModel::get($id); // 获取要删除的对象  不存在时返回 null    $user::destroy($id) //批量删时尽量用，返回记录条数，没有删除返回0
        if (!empty($data)) {
            if ($data->delete()) {
                return $this->success('删去成功', 'admin/WelfareType/index', '', 1);
            }
        }
        return $this->error('删去失败');
    }
    /**
     * 快捷操作
     * @return [type] [description]
     */
    public function quickedit()
    {
        //$id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
        if ($this->request->isPost()) {
            if (input('?post.status')) {
                $data = ['id' => Request::post('id/d'), 'status' => Request::post('status')]; //状态
            }
            $model = WelfareTypeModel::update($data);
            //echo $model;
            if (!empty($model)) {
                return $this->success('修改成功');
            } else {
                return $this->error('修改失败');
            }
        }
    }
}
